package com.OnTimeTeam.OnTime;

import java.util.ArrayList;

import com.google.android.gms.maps.model.LatLng;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;



/******************************************************************************
 * ClassName: 
 * MoreAbout:
 * 
 *
 *
 *****************************************************************************/
@SuppressWarnings("deprecation")
public class RouteDetailsTabHost extends TabActivity {
	 
	public RoadsQuery roadsQ;
	public ArrayList<LatLng> alRoute;
	
	/***Everything that happens within these brackets, happens when the activity is created.
    OnCreate is the first method which opens the application on the user side****/
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.show_route_details_tabhost);//set the view of the first screen
        
        String transportStr = "walking";
        
        if(getIntent().getExtras().getString("Transport").equals("Vehicle"))
        	transportStr = "driving";

        if(getIntent().getExtras().getString("Transport").equals("Transit") == false){
        	
        	RoutDetailsAsistance roadQueryOnBackground = new RoutDetailsAsistance(this);
        	roadQueryOnBackground.execute(getIntent().getExtras().getString("Origin"), getIntent().getExtras().getString("Dest"), transportStr);
        }
        else{ //transit
        	//need to create roadsQ from all data we have in intent (instead of roadsQ , get
        	// distance, duration, chosen track - create roadsQ from parameters and start ConfirmSmartAlarmTransit 
        	//instead of calling createTabs()
        
        	//create roadsQ from params
        	roadsQ = new TransitRoadsQuery(getIntent().getExtras().getString("duration"),getIntent().getExtras().getString("distance"),
        			(Track)getIntent().getExtras().getSerializable("track"));
        	
        	this.createTabs();
        	
        }
      
    }

 
    public void createTabs(){
    	
    	if((roadsQ == null) || (roadsQ.getTotalDistance() == null)) {
    		Toast.makeText(this, getResources().getString(R.string.errorServer), Toast.LENGTH_LONG).show();
    		finish();
    	}

    	
    	final TabHost tabHost = getTabHost();
    	
		TabSpec detailsTab = tabHost.newTabSpec("tid1");
		
		Drawable detailsDrawable = getResources().getDrawable( R.drawable.alarm_symb );
	    View detailsTabIndicator = LayoutInflater.from(this).inflate(R.layout.main_tabhost_item, getTabWidget(), false);
	    ImageView detailsIcon = (ImageView) detailsTabIndicator.findViewById(R.id.icon);
	    detailsIcon.setImageDrawable(detailsDrawable);
	    detailsIcon.setBackgroundResource(R.drawable.tabhost_selector);
	    detailsIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    
	    
	    Intent detailsIntent = new Intent(this, RouteDetails.class);
	    detailsIntent.putExtra("ArrivalHour", getIntent().getExtras().getString("ArrivalHour"));
	    detailsIntent.putExtra("ArrivalMinute", getIntent().getExtras().getString("ArrivalMinute"));
	    detailsIntent.putExtra("Hour", getIntent().getExtras().getString("Hour"));
	    detailsIntent.putExtra("Minute", getIntent().getExtras().getString("Minute"));
	    detailsIntent.putExtra("Origin", getIntent().getExtras().getString("Origin"));
	    detailsIntent.putExtra("Dest", getIntent().getExtras().getString("Dest"));
	    detailsIntent.putExtra("TTO", getIntent().getExtras().getString("TTO"));
	    detailsIntent.putExtra("Transport", getIntent().getExtras().getString("Transport"));
	    detailsIntent.putExtra("Distance", roadsQ.getTotalDistance());
	    
	    
		int hoursRouteDuration = 0;
		int minutesRouteDuration = 0;
		
		if(getIntent().getExtras().getString("Transport").equals("Bicycle"))
			minutesRouteDuration = (int)(((float)roadsQ.getTotalDurationNumeric())/2);
		
		else if(getIntent().getExtras().getString("Transport").equals("Vehicle")) 
		{
			minutesRouteDuration = roadsQ.getTotalDurationNumeric();
		}
		/*else
			minutesRouteDuration = roadsQ.getTotalDurationNumeric();
		 */
		String duration = "";
		
		if(getIntent().getExtras().getString("Transport").equals("Transit") == false)
		{
			while(minutesRouteDuration - 60 >= 0) 
			{
				minutesRouteDuration-=60;
				hoursRouteDuration+=1;
			}
		
			if(hoursRouteDuration == 0)
				duration = Integer.toString(minutesRouteDuration) + " " + getResources().getString(R.string.minutesLong);
			else if(minutesRouteDuration == 0)
				duration = Integer.toString(hoursRouteDuration) + " " + getResources().getString(R.string.hoursLong);
			else
				duration = Integer.toString(hoursRouteDuration) + " " + getResources().getString(R.string.hoursLong) + ", "+ Integer.toString(minutesRouteDuration) + " " + getResources().getString(R.string.minutesLong);	
		}
		else//transit
			duration = roadsQ.getTotalDuration();
			
		detailsIntent.putExtra("Duration", duration);

		Log.e("Duration", duration);
			
	    detailsTab.setContent(detailsIntent);
	    detailsTab.setIndicator(detailsTabIndicator);
		
	 if(RegularRoadsQuery.class.isInstance(roadsQ))
	 {
	    TabSpec mapTab = tabHost.newTabSpec("tid2");
    	
		Drawable mapDrawable = getResources().getDrawable( R.drawable.loc_grey);
	    View mapTabIndicator = LayoutInflater.from(this).inflate(R.layout.main_tabhost_item, getTabWidget(), false);
	    ImageView mapIcon = (ImageView) mapTabIndicator.findViewById(R.id.icon);
	    mapIcon.setImageDrawable(mapDrawable);
	    mapIcon.setBackgroundResource(R.drawable.tabhost_selector);
	    mapIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    
	    
		Intent mapIntent = new Intent(this, MapActivity.class);
		
		//Put all necessary route values
		mapIntent.putExtra("srcLat", ((RegularRoadsQuery) roadsQ).getSrcLat());
		mapIntent.putExtra("srcLng", ((RegularRoadsQuery) roadsQ).getSrcLng());
		mapIntent.putExtra("dstLat", ((RegularRoadsQuery) roadsQ).getDestLat());
		mapIntent.putExtra("dstLng", ((RegularRoadsQuery) roadsQ).getDestLng());
		mapIntent.putExtra("origin", getIntent().getExtras().getString("Origin"));
		mapIntent.putExtra("dest", getIntent().getExtras().getString("Dest"));
		mapIntent.putExtra("transport", getIntent().getExtras().getString("Transport"));
		mapIntent.putExtra("coords", ((RegularRoadsQuery) roadsQ).getCoordinatesDoubleArray());

	    mapTab.setContent(mapIntent);
	    mapTab.setIndicator(mapTabIndicator);	
		
	    tabHost.addTab(detailsTab);
	    tabHost.addTab(mapTab);
	 }
	 
	 else{
		
		 	TabSpec trackTab = tabHost.newTabSpec("tid2");
		 	Drawable trackDrawable = getResources().getDrawable( R.drawable.loc_grey);
		    View trackTabIndicator = LayoutInflater.from(this).inflate(R.layout.main_tabhost_item, getTabWidget(), false);
		    ImageView trackIcon = (ImageView) trackTabIndicator.findViewById(R.id.icon);
		    trackIcon.setImageDrawable(trackDrawable);
		    trackIcon.setBackgroundResource(R.drawable.tabhost_selector);
		    trackIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);
		    
			Intent detailedTrackIntent = new Intent(this, DetailedTrack.class);	
			detailedTrackIntent.putExtra("track", ((TransitRoadsQuery)roadsQ).getChosenTrack());
			//isLink = true --> context of route details is confirmation! (used for removing set+cancel buttons)
			detailedTrackIntent.putExtra("isLink", true);

		    trackTab.setContent(detailedTrackIntent);
		    trackTab.setIndicator(trackTabIndicator);	
			
		    
		    //tabHost.addTab(instructionsTab);
		    tabHost.addTab(detailsTab);
		    tabHost.addTab(trackTab);
			

		 
		 
		 
	 }
    	
    	
    }
       
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

}
